home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6012 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  69 lines

  1. Path: gail.ripco.com!mambuhl
  2. From: mambuhl@ripco.com (Martin Ambuhl)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What`s better & why
  5. Date: 22 Feb 1996 06:47:05 GMT
  6. Organization: Ripco Communications, Inc.
  7. Message-ID: <4gh3h9$d1b@gail.ripco.com>
  8. NNTP-Posting-Host: golden.ripco.com
  9.  
  10.  
  11. [Correction to earlier post included below (sorry)]
  12. Scott Hawley <shawley@connix.com>
  13. in <31297C5A.E6C@connix.com> asks:
  14.  
  15. >I was just curious what you all though about the following code.
  16. >Please tell me what is better (faster/Smaller) and why. Or does it make
  17. >any difference at all. I just though about this while I was programming
  18. >today?
  19.  
  20. >example 1:
  21. >                val = 1;
  22. >                if( what ever...)val = 0;
  23.  
  24. >or
  25. >example 2:
  26. >                if( what ever... )val = 0;
  27. >                else val = 1;
  28.  
  29. It all depends on your compiler.  With mine, there's no difference.
  30. Below notice the side-by-side comparison of three [now 4] functions and
  31. the code the compiler generates:
  32.  
  33. int func1(int x)    | int func2(int x)    |  int func3(int x)
  34. {                   | {                   |  {
  35.     int val;        |     int val;        |      int val;
  36.     val = 1;        |     if (x > 0)      |      val = x <= 0;
  37.     if (x > 0)      |         val = 0;    |      return val;
  38.         val = 0;    |     else            |  }
  39.     return val;     |         val = 1;    |
  40.                     |     return val; /* omitted in earlier post */
  41.                     | }                   |
  42.  
  43. .globl _func1       | .globl _func2       |   .globl _func3
  44. _func1:             | _func2:             |   _func3:
  45.     pushl %ebp      |     pushl %ebp      |       pushl %ebp
  46.     movl %esp,%ebp  |     movl %esp,%ebp  |       movl %esp,%ebp
  47.     cmpl $0,8(%ebp) |     cmpl $0,8(%ebp) |       cmpl $0,8(%ebp)
  48.     setle %al       |     setle %al       |       setle %al
  49.     andl $255,%eax  |     andl $255,%eax  |       andl $255,%eax
  50.     leave           |     leave           |       leave
  51.     ret             |     ret             |       ret
  52.                     |                     |
  53.  
  54.  [and here's the new fourth one, also the same code out]
  55. int func4(int x)              .globl _func4
  56. {                             _func4:
  57.     return x <= 0;                pushl %ebp
  58. }                                 movl %esp,%ebp
  59.                                   cmpl $0,8(%ebp)
  60.                                   setle %al
  61.                                   andl $255,%eax
  62.                                   leave
  63.                                   ret
  64.  
  65.                                                                             
  66. --
  67. * Martin Ambuhl       net: mambuhl@ripco.com
  68. * Chicago, IL (USA)    
  69.